using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Reflection;
using System.Text;

namespace Comics
{
	public sealed class SiteSettings
	{
		private static SiteSettings m_singleton;
		private string m_adminEmailAddress;
		private string m_supportEmailAddress;
		private string m_smtpServer;
		private int m_stmpPort;
		private string m_smtpUserName;
		private string m_smtpPassword;
		private string m_proxyServer;
		private int m_proxyPort;
		private string m_proxyUserName;
		private string m_proxyPassword;

		#region Settings

		public string AdminEmailAddress { get { return m_adminEmailAddress; } set { m_adminEmailAddress = value; } }
		public string SupportEmailAddress { get { return m_supportEmailAddress; } set { m_supportEmailAddress = value; } }
		public string SMTPServer { get { return m_smtpServer; } set { m_smtpServer = value; } }
        public int SMTPPort { get { return m_stmpPort; } set { m_stmpPort = value; } }
		public string SMTPUserName { get { return m_smtpUserName; } set { m_smtpUserName = value; } }
		public string SMTPPassword { get { return m_smtpPassword; } set { m_smtpPassword = value; } }
		public string ProxyServer { get { return m_proxyServer; } set { m_proxyServer = value; } }
        public int ProxyPort { get { return m_proxyPort; } set { m_proxyPort = value; } }
		public string ProxyUserName { get { return m_proxyUserName; } set { m_proxyUserName = value; } }
		public string ProxyPassword { get { return m_proxyPassword; } set { m_proxyPassword = value; } }

		#endregion Settings

		public static event EventHandler<EventArgs> Changed;

		public static SiteSettings Instance
		{
			get
			{
				if (m_singleton == null)
				{
					m_singleton = new SiteSettings();
				}
				return m_singleton;
			}
		}

		public SiteSettings() { LoadSettings(); }

		internal void LoadSettings()
		{
			Type _type = this.GetType();
			StringDictionary _settings = Services.Service.LoadSettings();
			foreach (string _key in _settings.Keys)
			{
				string _name = _key;
				string _value = _settings[_key];

				foreach (PropertyInfo _info in _type.GetProperties())
				{
					if (_info.Name.Equals(_name, StringComparison.OrdinalIgnoreCase))
					{
						_info.SetValue(this, Convert.ChangeType(_value, _info.PropertyType, CultureInfo.CurrentCulture), null);
					}
				}
			}
		}

		private static void OnChanged()
		{
			if (SiteSettings.Changed != null)
			{
				SiteSettings.Changed(null, new EventArgs());
			}
		}

		public void Save()
		{
			StringDictionary _settings = new StringDictionary();
			Type _type = this.GetType();

			foreach (PropertyInfo _info in _type.GetProperties())
			{
				try
				{
					if (_info.Name != "Instance")
					{
						object _value = _info.GetValue(this, null);
						string _valueString = _value.ToString();

						if (_value.Equals(null))
						{
							_valueString = String.Empty;
						}
						if (_value.Equals(Int32.MinValue))
						{
							_valueString = String.Empty;
						}
						if (_value.Equals(Single.MinValue))
						{
							_valueString = String.Empty;
						}

						_settings.Add(_info.Name, _valueString);
					}
				}
				catch { }
			}

			Services.Service.SaveSettings(_settings);

			OnChanged();
		}

		public string Version()
		{
			return GetType().Assembly.GetName().Version.ToString();
		}

	}
}
